﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiSearchResultObject
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Dynamic;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

internal class CUiSearchResultObject : DynamicObject, IDisposable
{
  private Dictionary<string, object> _properties = new Dictionary<string, object>();
  private string _cloneStableIdChain;
  private ITxObject _loadedObject;
  private TxTransformation _initialLocation;

  public event Action<CUiSearchResultObject> VisibilityChanged;

  public event Action<CUiSearchResultObject> LocationChanged;

  public CUiSearchResultObject(TcSoaModelObjectData modelObjectData, CUiSearchScope sourceScope)
  {
    this.Uid = modelObjectData.uid;
    this.RevisionType = modelObjectData.revisionType;
    this.SourceScope = sourceScope;
    this.SourceObjectIcon = TxIconManager.the().GetObjectIcon(modelObjectData.revisionType);
  }

  public Image SourceObjectIcon { get; private set; }

  public CUiSearchScope SourceScope { get; private set; }

  public string Uid { get; private set; }

  public string RevisionType { get; private set; }

  public int RowIndex { get; set; }

  public bool? IsExistingObject { get; private set; }

  public bool IsLoaded { get; private set; }

  public TxTransformation LastLocation { get; private set; }

  public bool IsAtInitialLocation
  {
    get
    {
      bool atInitialLocation = true;
      if (this.IsLoaded && this.LocatableObject != null && TxTransformation.op_Inequality(this.LastLocation, (TxTransformation) null))
        atInitialLocation = false;
      return atInitialLocation;
    }
  }

  public string CloneStableIdChain
  {
    get
    {
      if (this._cloneStableIdChain == null)
        this._cloneStableIdChain = CApTcIntegrationWrapper.CalculateCloneStableIdChain(this.Uid);
      return this._cloneStableIdChain;
    }
  }

  public ITxDisplayableObject DisplayableObject => this._loadedObject as ITxDisplayableObject;

  public ITxLocatableObject LocatableObject => this._loadedObject as ITxLocatableObject;

  public TxDisplayableObjectVisibility Visibility
  {
    get
    {
      TxDisplayableObjectVisibility visibility = (TxDisplayableObjectVisibility) 2;
      if (this.IsLoaded)
        visibility = this.DisplayableObject == null ? (TxDisplayableObjectVisibility) 3 : this.DisplayableObject.Visibility;
      return visibility;
    }
  }

  public override bool TryGetMember(GetMemberBinder binder, out object result)
  {
    return this._properties.TryGetValue(binder.Name, out result);
  }

  public override bool TrySetMember(SetMemberBinder binder, object value)
  {
    this._properties[binder.Name] = value;
    return true;
  }

  public void LoadProperties(IDictionary<string, string> newProperties)
  {
    this._properties.Clear();
    foreach (KeyValuePair<string, string> newProperty in (IEnumerable<KeyValuePair<string, string>>) newProperties)
      this._properties.Add(newProperty.Key, (object) newProperty.Value);
  }

  public string GetProperty(string propertyName) => this._properties[propertyName].ToString();

  public void SetLoadData(ITxObject loadedObject, bool isExisting)
  {
    if (this.IsLoaded)
    {
      if (this.DisplayableObject != null)
        TxDisplayableObjectEx.StopEmphasizeLightBlueObject(this.DisplayableObject);
      this.ClearLoadData();
    }
    this.IsLoaded = true;
    this._loadedObject = loadedObject;
    this.IsExistingObject = new bool?(isExisting);
    if (this.LocatableObject != null && TxTransformation.op_Equality(this.LastLocation, (TxTransformation) null))
      this._initialLocation = this.LocatableObject.AbsoluteLocation;
    if (this.LocatableObject != null && TxTransformation.op_Inequality(this.LastLocation, (TxTransformation) null) && TxTransformation.op_Inequality(this.LocatableObject.AbsoluteLocation, this.LastLocation))
      this.LocatableObject.AbsoluteLocation = this.LastLocation;
    if (this.DisplayableObject != null)
      TxDisplayableObjectEx.EmphasizeLightBlueObject(this.DisplayableObject);
    this.RegisterForEvents();
  }

  public void Display()
  {
    if (!this.IsLoaded || this.DisplayableObject == null)
      return;
    this.DisplayableObject.Display();
  }

  public void Blank()
  {
    if (!this.IsLoaded || this.DisplayableObject == null)
      return;
    this.DisplayableObject.Blank();
  }

  public void ToggleDisplayState()
  {
    if (!this.IsLoaded || this.DisplayableObject == null || this.DisplayableObject.Visibility == 3)
      return;
    if (this.DisplayableObject.Visibility == 2)
      this.DisplayableObject.Display();
    else
      this.DisplayableObject.Blank();
  }

  public void RestoreInitialLocation()
  {
    if (this.LocatableObject == null)
      return;
    this.LocatableObject.AbsoluteLocation = this._initialLocation;
    this.LastLocation = (TxTransformation) null;
  }

  private void RegisterForEvents()
  {
    if (!this.IsLoaded)
      return;
    if (this.DisplayableObject != null)
    {
      this.DisplayableObject.VisibilitySet += new TxDisplayableObject_VisibilitySetEventHandler(this.DisplayObject_VisibilitySet);
      ((ITxObject) this.DisplayableObject).Deleted += new TxObject_DeletedEventHandler(this.DisplayableObject_Deleted);
    }
    if (this.LocatableObject == null)
      return;
    this.LocatableObject.AbsoluteLocationChanged += new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.LocatableObject_AbsoluteLocationChanged);
  }

  private void DisplayObject_VisibilitySet(
    object sender,
    TxDisplayableObject_VisibilitySetEventArgs args)
  {
    if (this.VisibilityChanged == null)
      return;
    this.VisibilityChanged(this);
  }

  private void DisplayableObject_Deleted(object sender, TxObject_DeletedEventArgs args)
  {
    this.ClearLoadData();
  }

  private void LocatableObject_AbsoluteLocationChanged(
    object sender,
    TxLocatableObject_AbsoluteLocationChangedEventArgs args)
  {
    this.LastLocation = !TxTransformation.op_Inequality(this.LocatableObject.AbsoluteLocation, this._initialLocation) ? (TxTransformation) null : this.LocatableObject.AbsoluteLocation;
    if (this.LocationChanged == null)
      return;
    this.LocationChanged(this);
  }

  private void UnregisterFromEvents()
  {
    if (!this.IsLoaded)
      return;
    if (this.DisplayableObject != null)
    {
      this.DisplayableObject.VisibilitySet -= new TxDisplayableObject_VisibilitySetEventHandler(this.DisplayObject_VisibilitySet);
      ((ITxObject) this.DisplayableObject).Deleted -= new TxObject_DeletedEventHandler(this.DisplayableObject_Deleted);
    }
    if (this.LocatableObject == null)
      return;
    this.LocatableObject.AbsoluteLocationChanged -= new TxLocatableObject_AbsoluteLocationChangedEventHandler(this.LocatableObject_AbsoluteLocationChanged);
  }

  private void ClearLoadData()
  {
    if (!this.IsLoaded)
      return;
    this.UnregisterFromEvents();
    this._loadedObject = (ITxObject) null;
    this.IsExistingObject = new bool?();
    this.IsLoaded = false;
  }

  public void Dispose()
  {
    if (this.DisplayableObject != null)
      TxDisplayableObjectEx.StopEmphasizeLightBlueObject(this.DisplayableObject);
    this.ClearLoadData();
  }
}
